﻿using System;
using System.Net.Sockets;
using System.Text;
using static System.Console;

namespace udp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                if (args.Length != 3)
                {
                    WriteLine("Usage: udp host port message");
                    return;
                }

                string aHost = args[0];
                if (!int.TryParse(args[1], out int aPort))
                {
                    WriteLine("Invalid port!\nUsage: udp host port message");
                    return;
                }
                string aMessage = args[2];
                Encoding aEncoding = Encoding.UTF8;
                byte[] aBytes = aEncoding.GetBytes(aMessage);

                using (UdpClient aUdpClient = new UdpClient(aHost, aPort))
                {
                    aUdpClient.Send(aBytes, aBytes.Length);
                }
            }
            catch (Exception ex)
            {
                WriteLine(ex.Message);
            }
        }
    }
}
